package com.bakery.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bakery.entity.StockRecord;
import com.bakery.mapper.StockRecordMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 库存变动记录Service
 *
 * @author bakery
 */
@Service
public class StockRecordService extends ServiceImpl<StockRecordMapper, StockRecord> {
    
    /**
     * 分页查询库存变动记录
     */
    public Page<StockRecord> getStockRecordPage(Integer page, Integer size, Long breadId, 
                                                 LocalDateTime startTime, LocalDateTime endTime) {
        LambdaQueryWrapper<StockRecord> wrapper = new LambdaQueryWrapper<>();
        
        if (breadId != null) {
            wrapper.eq(StockRecord::getBreadId, breadId);
        }
        
        if (startTime != null) {
            wrapper.ge(StockRecord::getCreateTime, startTime);
        }
        
        if (endTime != null) {
            wrapper.le(StockRecord::getCreateTime, endTime);
        }
        
        wrapper.orderByDesc(StockRecord::getCreateTime);
        
        return page(new Page<>(page, size), wrapper);
    }
}

